package com.whfc.wxmp.api.fvs.controller;

import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.wxmp.annotation.CurrentWxUser;
import com.whfc.fuum.entity.WxUser;
import com.whfc.fvs.dto.FvsDeviceDTO;
import com.whfc.fvs.dto.FvsStreamDTO;
import com.whfc.fvs.dto.FvsTokenDTO;
import com.whfc.fvs.param.FvsDeviceListParam;
import com.whfc.wxmp.api.fvs.service.WxFvsDeviceService;
import com.whfc.wxmp.util.RequestConetxtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 视频监控设备
 *
 * @author : qzexing
 * @version : 1.0
 * @date : 2020/5/25 17:37
 */
@RestController
@RequestMapping("/mp/api/fvs/device")
public class WxFvsDeviceController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxFvsDeviceService wxFvsDeviceService;


    @GetMapping("/list")
    public Result list(@CurrentWxUser WxUser wxUser,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam(value = "groupId", required = false) Integer groupId,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam(value = "deviceStatus", required = false) Integer deviceStatus,
                       @RequestParam(value = "webcamType", required = false) Integer webcamType,
                       @RequestParam(value = "keyword", required = false) String keyword
                       ) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "视频监控设备列表", RequestConetxtUtil.getParameters());
        FvsDeviceListParam param = new FvsDeviceListParam();
        param.setDeptId(deptId);
        param.setPageNum(pageNum);
        param.setPageSize(pageSize);
        param.setGroupId(groupId);

        param.setDeviceStatus(deviceStatus);
        param.setWebcamType(webcamType);
        param.setKeyword(keyword);

        PageData<FvsDeviceDTO> pageData = wxFvsDeviceService.list(param);
        return ResultUtil.success(pageData);
    }


    @GetMapping("/stream/url")
    public Result streamUrl(@CurrentWxUser WxUser wxUser,
                            @RequestParam("deviceId") Integer deviceId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取直播地址", RequestConetxtUtil.getParameters());
        FvsStreamDTO streamUrl = wxFvsDeviceService.getStreamUrl(deviceId);
        return ResultUtil.success(streamUrl);
    }


    @GetMapping(value = "/token")
    public Result token(@CurrentWxUser WxUser wxUser,
                        @RequestParam("deptId") Integer deptId,
                        @RequestParam("platform") Integer platform) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取视频监控token", RequestConetxtUtil.getParameters());
        FvsTokenDTO tokenDTO = wxFvsDeviceService.getToken(deptId, platform);
        return ResultUtil.success(tokenDTO);
    }

    @PostMapping("/ptz")
    public Result ptz(@CurrentWxUser WxUser wxUser,
                      @RequestParam("deviceId") Integer deviceId,
                      @RequestParam("ptzOption") Integer ptzOption) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "视频监控设备云台操作", RequestConetxtUtil.getParameters());
        wxFvsDeviceService.ptz(deviceId, ptzOption);
        return ResultUtil.success();
    }

}
